﻿using System;
using System.Windows.Forms;
using Calendar.Domain;

namespace Calendar.GUI
{
    public delegate void UpdateHandler();
    public partial class WorkRecordForm : WorkForm
    {
        public UpdateHandler UpdateEvent = null;
        WorkHandler _workHandler;
        WorkRecordHandler _workRecordHandler;
        int _id;

        public WorkRecordForm(int id, WorkHandler workHandler)
        {
            InitializeComponent();
            _workHandler = workHandler;
            _workRecordHandler = new WorkRecordHandler(_workHandler.GetName(id));
            _workRecordHandler.UpdateRecordEvent += UpdateRecord;
            _id = id;
            UpdateComponent();
        }

        private void UpdateComponent()
        {
            workNameTextBox.Text = _workHandler.GetName(_id);
            startDateTimePicker.Value = _workHandler.GetStartTime(_id);
            deadlineDateTimePicker.Value = _workHandler.GetDeadline(_id); ;
            emergancyLevelComboBox.SelectedIndex = _workHandler.GetEmergencyLevel(_id);
            progressTrackBar.Value = _workHandler.GetProgress(_id);
            percentageLabel.Text = progressTrackBar.Value.ToString() + " %";
            _workRecordHandler.LoadRecord();
        }

        public void UpdateRecord(string record)
        {
            workRecordListBox.Items.Add(record);
            totalTimeLabel.Text = _workRecordHandler.TotalTimeLabel;
            workRecordListBox.SelectedIndex = _workRecordHandler.RecordSelectedIndex;
        }

        private void PomodoroButton_Click(object sender, EventArgs e)
        {
            PomodoroForm pomodoroForm = new PomodoroForm(_workRecordHandler);
            pomodoroForm.StartPosition = FormStartPosition.Manual;
            pomodoroForm.Location = new System.Drawing.Point(700,100);
            pomodoroForm.SetTimeAllocation(workRecordListBox.SelectedIndex);
            pomodoroForm.Width = 300;
            pomodoroForm.Show();          
        }

        private void workNameTextBox_TextChanged(object sender, EventArgs e)
        {
            _workHandler.SetName(_id, workNameTextBox.Text);
        }

        private void startDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            _workHandler.SetStartTime(_id, startDateTimePicker.Value);
        }

        private void deadlineDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            _workHandler.SetDeadline(_id, deadlineDateTimePicker.Value);
        }

        private void emergancyLevelComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            _workHandler.SetEmergancyLevel(_id, emergancyLevelComboBox.SelectedIndex);
        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            UpdateEvent.Invoke();
        }

        private void progressTrackBar_Scroll(object sender, EventArgs e)
        {
            _workHandler.SetProgress(_id, progressTrackBar.Value);
            UpdateEvent.Invoke();
        }

        private void workRecordListBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && workRecordListBox.Items.Count > 0)
            {
                _workRecordHandler.DeleteRecord(workRecordListBox.SelectedIndex);
                workRecordListBox.Items.Remove(workRecordListBox.SelectedItem);
                totalTimeLabel.Text = _workRecordHandler.TotalTimeLabel;
            } 
        }


    }
}
